<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="searchForm"><!--:inline="true"表单横向-->
				<el-form-item>
					<el-input v-model="searchForm.name" placeholder="部门名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getUsers">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="departments" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" :stripe="true">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index">
			</el-table-column>
			<el-table-column prop="name" label="部门名称" align="center">
			</el-table-column>
			<!--<el-table-column prop="state" label="部门状态"  :formatter="formatSex" >-->
			<el-table-column prop="state" label="部门状态" sortable align="center">
				<template scope="scope">
					<span v-if="scope.row.state=='-1'" style="color: red">停用</span>
					<span v-if="scope.row.state=='0'" style="color: green">正常</span>
				</template>
			</el-table-column>
			<el-table-column prop="sn" label="部门描述"  align="center">
			</el-table-column>
			<el-table-column prop="dirPath" label="部门层级"  align="center">
			</el-table-column>
			<el-table-column prop="emp.username" label="部门经理" align="center">
			</el-table-column>
			<el-table-column prop="par.name" label="上级部门" align="center">
			</el-table-column>
			<el-table-column label="操作" >
				<template scope="scope">
					<el-button type="primary" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增编辑公共界面-->
		<el-dialog :title="title" v-model="UserFormVisible" :close-on-click-modal="false" v-loading="UserLoading">
			<el-form :model="UserForm" label-width="80px" :rules="UserFormRules" ref="UserForm">
				<el-form-item label="部门名称" prop="name">
					<el-input v-model="UserForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="部门状态">
					<el-radio-group v-model="UserForm.state">
						<el-radio class="radio" :label="0">正常</el-radio>
						<el-radio class="radio" :label="-1">禁止</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="部门描述" prop="sn">
					<el-input v-model="UserForm.sn" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item  label="部门层级" prop="dirPath">
					<el-input v-model="UserForm.dirPath" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item  label="上级部门" prop="par">
					<template>
						<el-select v-model="UserForm.par" value-key="id" placeholder="请选择">
							<el-option
									v-for="item in pars"
									:key="item.id"
									:label="item.name"
									:value="item"
									:disabled="item.state == -1">
							</el-option>
						</el-select>
					</template>
				</el-form-item>
				<el-form-item label="部门经理" prop="emp">
					<template>
						<el-select v-model="UserForm.emp" value-key="id" placeholder="请选择">
							<el-option
									v-for="item in employees"
									:key="item.id"
									:label="item.username"
									:value="item"
									:disabled="item.state == -1">
							</el-option>
						</el-select>
					</template>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer" align="center">
				<el-button type="danger" @click.native="UserFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="UserSubmit" :loading="UserLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';
	export default {
		data() {
			return {
                searchForm: {
					name: ''
				},
                departments: [],
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列

				UserFormVisible: false,//编辑界面是否显示
				UserLoading: false,
				UserFormRules: {
					name: [
						{ required: true, message: '部门名称不能为空', trigger: 'blur' }
					],
					sn:[
						{required: true, message: '部门描述不能为空', trigger: 'blur'}
					]
				},
				//新增编辑界面数据
				UserForm: {
					name: '',
					state:'',
					sn: '',
					dirPath: '',
					emp:'',
					par:'',
				},
				employees:[],
				pars:[],
				title:'',
			}
		},
		methods: {
			handleCurrentChange(val) {//获取所在页码
				this.page = val;
				this.getUsers();//点击页码时调用钩子方法
			},
			//获取用户列表
			getUsers() {
			    //console.debug(this.searchForm)
				let para = {
					currentPage: this.page,
					keywords:this.searchForm.name
				};
				//打开加载转圈圈的效果
				this.listLoading = true;
				//NProgress.start();
				//前端模拟数据方法
				/*getUserListPage(para).then((res) => {
					this.total = res.data.total;
					this.departments = res.data.users;
					this.listLoading = false;
					//NProgress.done();
				});*/
				//console.debug(this.page);
				//查询数据
				this.$http.get("/drop_down/employee").then(res=>{
					this.employees = res.data;
				});
				this.$http.get("/drop_down/pars").then(res=>{
					this.pars = res.data;
				});
				this.$http.post("/department",para).then(res=>{
				    this.departments=res.data.data;
				    this.total=res.data.total;
                    //关闭加载的效果
                    this.listLoading = false;
				   //console.debug(res);
				});

			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.id };
					//removeUser(para).then((res) => {
					this.$http.delete("/department/"+para.id).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						if (res.data.success){
							this.$message({
								message: '删除成功',
								type: 'success'
							});
						}else {
							this.$message({
								message: "删除失败"+res.data.message,
								type: 'warning'
							});
						}
						this.getUsers();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.UserFormVisible = true;
				this.title='编辑部门';
				this.UserForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.UserFormVisible = true;
				this.title='新增部门';
				this.UserForm = {
					state:0,//状态默认正常
				};
			},
			//编辑和新增提交
			UserSubmit: function () {
				//验证当前的表单是否符合验证规则
				this.$refs.UserForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.UserLoading = true;
							//NProgress.start();
							//把表单的数据组装为json
							let para = Object.assign({}, this.UserForm);
							console.debug(para)
							this.$http.put('/department',para).then((res) => {
								this.UserLoading = false;
								//NProgress.done();
								if (res.data.success){
									this.$message({
										message: '提交成功',
										type: 'success'
									});
								}else {
									this.$message({
										message: "更新失败"+res.data.message,
										type: 'warning'
									});
								}
								//重置表单-调用现成的方法 清空数据
								this.$refs['UserForm'].resetFields();
								//关闭 更新界面
								this.UserFormVisible = false;
								//重新刷新页面数据 钩子方法
								this.getUsers();
							});
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				var names = this.sels.map(item => item.name).toString();
				this.$confirm('确认删除选中记录 '+ names +' 吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					console.debug(ids);
					let para = { ids: ids };
					this.$http.post('/department/dels',ids).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						if (res.data.success){
							this.$message({
								message: '删除成功',
								type: 'success'
							});
						}else {
							this.$message({
								message: "删除失败"+res.data.message,
								type: 'warning'
							});
						}
						this.getUsers();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
		    //当页面加载完以后。执行的方法
			this.getUsers();
		}
	}

</script>

<style scoped>

</style>